<template>
  <dialog-editor ref="dialog" :width="98" :top='top' :col-flag-func="true" close-on-press-escape></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'
import {isAuth} from '../../../utils'
// import {getFormatDateTime} from '@/utils'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      top: '7vh',
/*      stepsConfigRow: {
        steps: [
          {
            title: '步骤1',
            Attrs: ['orderNo','productId', 'clientId' ,'clientProductNo','deviceId']
          },
          {
            title: '步骤2',
            Attrs: ['orderTime', 'deliveryTime', 'orderProductNumber', 'boxSupplyWay', 'productWeight', 'weightScope','volumeScope','deepStatus', 'deepCategoryList', 'customerProductStyle', 'bottleCapSuit']
          },
          {
            title: '步骤3'
          }
        ]
      },*/
      teamLeadApprovalAuthName: 'order:orderdetail:boxOrder',
      dataFormConfig: {
        id: {
          hidden: true
        },

        orderNo: {
          label: '订单编号',
          disabled: true
        },
        productId: {
          label: '产品',
          type: 'list',
          selectCallback: this.productSelectCallback,
          filterable: true,
          list: [],
          placeholder: '请选择产品(若是配套组合，首选 光瓶)',
          isNeedAdd: true,
          addNewItemFunc: this.addNewProduct,
          isNeedLook: true,
          lookItemInfoFunc: this.lookProductInfo
        },
        productCategoryId: {
          hidden: true
        },
        unitPrice: {
          label: '单价',
          type: 'number',
          max: 10000,
          min: 0,
          default: 0
        },
        orderProductNumber: {
          label: '订单数量',
          type: 'number',
          max: 10000000,
          selectCallback: this.orderProductNumberChangedHandle
        },

        qualityGrade: {
          label: '质量等级',
          type: 'radio',
          radioList: [
            {label: 'A', text: 'A'},
            {label: 'B', text: 'B'},
            {label: 'C', text: 'C'}
          ],
          default: 'A'
        },
        productWeight: {
          label: '标准克重',
          type: 'number',
          selectCallback: this.productWeightChangedHandle,
          max: 1000
        },
        //detailServiceById

        // weightScope: {
        //   label: '克重误差范围(±)',
        //   default: '±'
        // },
        // deviceId: {
        //   label: '拟生产厂商',
        //   type: 'list',
        //   filterable: true,
        //   list: [],
        //   otherInfo: {label: '克重范围：', attrName: 'weightRange'},
        //   placeholder: '请选择光瓶拟生产厂商'
        // },

        // groupType: {
        //   label: '组合',
        //   default: 1
        // },
        // orderIssuesNumber: {
        //   label: '损耗数量',
        //   type: 'number',
        //   disabled: () => this.dataForm.status === 4||this.dataForm.status===30||this.dataForm.status===32,
        //   max: 10000000
        // },
        needPurchaseType: {
          label: '订单模式',
          type: 'radio',
          radioBorder: false,
          radioList: [
            {text: '通货', label: 0},
            {text: '现模', label: 1},
            {text: '开模（定制-只能单个产品下）', label: 2}
          ],
          changedCallback: this.needPurchaseTypeSelectCallback,
          default: 1
        },
        productVolume: {
          label: '标准容量',
          type: 'number',
          max: 1000
        },
        // customerRequire: {label: '客户规格描述'},
        // volumeScope: {
        //   label: '容量范围(±)',
        //   default: '±'
        // },
        // bottleCraft: {
        //   label: '工艺'
        // },



  /*      purchaseNumber: {
          reply: () => this.dataForm.needPurchaseType != 0,
          hidden: true,
          label: '采购数量',
          type: 'number',
          default: 0
        },*/



        relateDeepStatus: {
          reply: () => (this.dataForm.needPurchaseType == 2&&this.dataForm.productCategoryId !=1)||(this.dataForm.needPurchaseType==1)||(this.dataForm.needPurchaseType==0),
          label: '是否采购配件',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 0
        },
        relateDeepCategoryList: {
          reply: () => this.dataForm.relateDeepStatus,
          label: '配件工艺',
          type: 'listAndListAndInputAndNumber',
          listPlaceholder: '工艺方式',
          numberPlaceholder: '备注（颜色、口径）',
          inputNamePlaceholder: '请输入备注',
          listName: 'relateDeepCategoryId',
          numberName: 'customerRequire',
          radioName: 'purchaseNumber',
          selectCallback: this.productSelectCallback2,
          //  radioBorder: false,
          // radioList: [
          //   {text: '在厂', label: 0, tagType: 'success'},
          //   {text: '不在厂', label: 1}
          // ],
          /*          otherAttrs: ['id'],*/
          list: []
        },


        deepStatus: {
          reply: () => this.dataForm.productCategoryId ==1,
          label: '光瓶深加工',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 0
        },
        deepCategoryList: {
          reply: () => this.dataForm.deepStatus==1,
          label: '光瓶深加工方式',
          type: 'listAndNumberAndRadio',
          listPlaceholder: '请选择加工方式',
          numberPlaceholder: '请输入深加工备注（颜色）',
          listName: 'deepCategoryIdList',
          numberName: 'customerRequire',
          radioName: 'purchaseNumber',
          list: []
        },

        // deepCategoryList: {
        //   reply: () => this.dataForm.deepStatus,
        //   label: '深加工方式',
        //   type: 'listAndInput',
        //   listPlaceholder: '请选择加工方式',
        //   inputPlaceholder: '请输入色号',
        //   listName: 'deepCategoryId',
        //   inputName: 'colorNo',
        //   numberName: 'unitPrice',
        //   otherAttrs: ['id'],
        //   list: []
        // },

        // deepCategoryList: {
        //   reply: () => this.dataForm.deepStatus,
        //   label: '深加工方式',
        //   type: 'listAndNumberAndRadio',
        //   listPlaceholder: '请选择加工方式',
        //   numberPlaceholder: '请输入深加工备注',
        //   listName: 'deepCategoryId',
        //   numberName: 'unitPrice',
        //   radioName: 'manufacturerType',
        //   radioBorder: false,
        //   radioList: [
        //     {text: '在光瓶厂加工', label: 0, tagType: 'success'},
        //     {text: '其他厂商', label: 1}
        //   ],
        //   otherAttrs: ['id'],
        //   list: []
        // },
        deepProductImage: {
          reply: () => this.dataForm.deepStatus==1,
          label: '光瓶深加工样图',
          type: 'uploadImage',
          imageType: 'uploadProductImage'
        },
        relateDeepProductImage: {
          reply: () => this.dataForm.relateDeepStatus,
          label: '配件工艺样图',
          type: 'uploadImage',
          imageType: 'uploadProductImage'
        },

        proofingStatus: {
          label: '是否打样',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        },

        proofingAmount:{
          label: '打样费用',
          type: 'number',
          default: 0
        },
        modelAmount:{
          label: '模具费用',
          type: 'number',
          default: 0
        },
        designModelImage:{
          reply: () => this.dataForm.needPurchaseType==2,
          label: '设计图',
          type: 'uploadImage',
          imageType: 'uploadProductImage'
        },
        sampleImage: {
          label: '产品样图',
          type: 'uploadImage',
          imageType: 'uploadProductImage'
        },
        productImageShow: {
          label: '图片展示',
          type: 'uploadImage',
          imageType: 'uploadProductPaper'
        },
        boxPackImage: {
          label: '箱唛',
          type: 'uploadScreenshot',
          imageType: 'uploadProductImage'
        },
        historyUnitPrice: {
          label: '历史价格'
        },
        remark: {
          label: '备注',
          placeholder: '请输入备注'
        },
        countNumber: {
          label: '库存数量',
          type: 'number',
          default: 0
        },

        status: {
          label: '订单状态',
          list: [],
          type: 'list',
          default: 5
        },
      }
    }
  },
  methods: {
    /**
     * 初始化瓶配信息列表
     * @param attrName 瓶配信息属性名
     */
    initBottleSuitList(attrName = 'bottleSuitList') {
      mRequest('systemApi/getSysDictLabelTreeList', {}, (res) => {
        this.dataFormConfig[attrName].list = this.R.pathOr([], ['data', 'list'], res)
        this.dataFormConfig['bottleCapList'].list = this.R.pathOr([], ['data', 'list'], res)
        this.dataFormConfig['bottleSprayHeadList'].list = [{'id':2, 'name': 2}]
        this.dataFormConfig['bottleSprayHeadList']['listName'].bottleSuitInfo=2
      })
    },
    // /**
    //  * 初始化盖配信息列表
    //  * @param attrName 盖配信息属性名
    //  */
    initCapSuitInfoList(attrName = 'capSuitInfoList') {
      mRequest('systemApi/getCapSuitInfoList', {}, (res) => {
        this.dataFormConfig[attrName].list = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    /**
     * 成模数量变化时的回调
     */
    successMoChangedHandle() {
      this.dataForm.successMo = this.dataForm.successMo || 0
      this.dataForm.primaryMo = this.dataForm.successMo|| 0
      this.dataForm.jiaZi = this.dataForm.successMo|| 0
      this.dataForm.kouZi = this.dataForm.successMo|| 0
      this.dataForm.lengQue = this.dataForm.successMo|| 0
      this.dataForm.louDou = this.dataForm.successMo|| 0
      this.dataForm.menTou = this.dataForm.successMo|| 0
      this.dataForm.qiTou = this.dataForm.successMo|| 0
      this.dataForm.xinZi = this.dataForm.successMo|| 0
    },
    /**
     * 产品重量发生便话
     * @param value
     */
    productWeightChangedHandle(value) {
      console.log("  productWeightChange "+  value)
      if (window.R.isNil(value)) {
        return
      }

      mRequest('productApi/getProductDeviceInfoByWeight', {productWeight: value}, (res) => {
        const productDevice = this.R.path(['data', 'productDevice'], res)

        this.dataForm['deviceId'] = productDevice

      })

    },
    /**
     * 产品选择后的回调
     * @param value 选择的产品
     */
    productSelectCallback(value) {
      if (window.R.isNil(value)) {
        return
      }
      let clientId = this.dataForm.clientId

      mRequest('productApi/getProductInfoByClientId', {id: value}, (res) => {
        const productInfo = this.R.path(['data', 'productInfo'], res)

        this.dataForm['productWeight'] = productInfo['productWeight']
        this.dataForm['productCategoryId'] = productInfo['productCategoryId']
        this.dataForm['bottleOutDiameter'] = productInfo['bottleOutDiameter']
        this.dataForm['bottleCapSuit'] = productInfo['productAssort']
        this.dataForm['weightScope'] = this.R.isNil(productInfo['weightScope']) ? '±' : productInfo['weightScope']
        this.dataForm['headNeckHeight'] = productInfo['headNeckHeight']
        this.dataForm['productVolume'] = Number(productInfo['productVolume'])
        this.dataForm['volumeScope'] = this.R.isNil(productInfo['volumeScope']) ? '±' : productInfo['volumeScope']
        this.dataForm['bottleLength'] = productInfo['bottleLength']
        this.dataForm['bottleWeight'] = productInfo['bottleWeight']
        this.dataForm['bottleHeight'] = productInfo['bottleHeight']
        this.dataForm['countNumber'] = productInfo['countNumber']
        this.dataForm['historyUnitPrice'] = productInfo['historyUnitPrice']
        this.dataForm['productImageShow'] = productInfo['productImageId']
        if (this.dataForm.productCategoryId != 1) {
          this.dataForm['relateDeepStatus'] = 1
          this.dataForm['relateDeepCategoryList'][0].productId = value
          this.dataForm.deepStatus=0
        }
        if (this.dataForm.needPurchaseType == 2 && 1==this.dataForm.productCategoryId ) {
          this.dataForm.relateDeepStatus = 0
        }
      })


    },
    productSelectCallback2(value) {
      if (window.R.isNil(value)) {
        return
      }
      if (value == this.dataForm['relateDeepCategoryList'][0].productId) {
        return
      }
      let clientId = this.dataForm.clientId

      mRequest('productApi/getProductInfoByClientId', {id: value}, (res) => {
        const productInfo = this.R.path(['data', 'productInfo'], res)

     //   this.dataForm['productWeight'] = productInfo['productWeight']
      //  this.dataForm['productCategoryId'] = productInfo['productCategoryId']
      //   this.dataForm['bottleOutDiameter'] = productInfo['bottleOutDiameter']
      //   this.dataForm['bottleCapSuit'] = productInfo['productAssort']
      //   this.dataForm['weightScope'] = this.R.isNil(productInfo['weightScope']) ? '±' : productInfo['weightScope']
      //   this.dataForm['headNeckHeight'] = productInfo['headNeckHeight']
      //   this.dataForm['productVolume'] = Number(productInfo['productVolume'])
      //   this.dataForm['volumeScope'] = this.R.isNil(productInfo['volumeScope']) ? '±' : productInfo['volumeScope']
      //   this.dataForm['bottleLength'] = productInfo['bottleLength']
      //   this.dataForm['bottleWeight'] = productInfo['bottleWeight']
      //   this.dataForm['bottleHeight'] = productInfo['bottleHeight']
      //   this.dataForm['countNumber'] = productInfo['countNumber']
      //   this.dataForm['historyUnitPrice'] = productInfo['historyUnitPrice']
        this.dataForm['productImageShow'] = productInfo['productImageId']
      })
    },
    needPurchaseTypeSelectCallback(value) {
      if (value == 2 && 1==this.dataForm.productCategoryId ) {
        this.dataForm.relateDeepStatus = 0
      }
      if (value == 0) {
        this.dataForm.proofingStatus = 0
      }else {
        this.dataForm.proofingStatus = 1
      }
    },
    //商品信息
    commoditySelectCallback() {

    },
    addNewCommodity() {
      const cb = (newValue) => {
        this.initCommodityInfoList('commodityId')
        this.dataForm.commodityId = newValue.id
      }
      this.$parent.openCommodityInfoDialog(cb)
    },
    lookCommodityInfo() {

    },
    //盖子
    addNewBottleCap() {

      const cb = (newValue) => {
        this.initBottleCapInfoList('bottleCapId')
        this.dataForm.bottleCapId = newValue.id
      }
      this.$parent.openBottleCapInfoDialog(cb)
    },
    capIdSelectCallback() {

    },
    lookBottleCapInfo() {

    },

    //喷头
    addNewSprayHead() {

      const cb = (newValue) => {
        this.initSprayHeadInfoList('bottleSprayHeadId')
        this.dataForm.bottleSprayHeadId = newValue.id
      }
      this.$parent.openSprayHeadInfoDialog(cb)
    },
    sprayHeadIdSelectCallback() {

    },
    lookSprayHeadInfo() {

    },
    //肩套
    addNewBottleNeckRing() {

      const cb = (newValue) => {
        this.initNeckRingInfoList('bottleNeckRingId')
        this.dataForm.bottleNeckRingId = newValue.id
      }
      this.$parent.openNeckRingInfoDialog(cb)


    },
    orderProductNumberChangedHandle() {
      if (this.dataForm.productCategoryId != 1) {
        if (this.dataForm.relateDeepCategoryList.length ==1) {
          this.dataForm['relateDeepCategoryList'][0].purchaseNumber = this.dataForm.orderProductNumber
        }
      }
      this.dataForm.purchaseNumber = this.dataForm.orderProductNumber
    },
    bottleNeckRingIdSelectCallback() {

    },
    lookBottleNeckRingInfo() {

    },
    //中套
    addNewCenterCover() {
      const cb = (newValue) => {
        this.initCenterCoverInfoList('centerCoverId')
        this.dataForm.centerCoverId = newValue.id
      }
      this.$parent.openCenterCoverInfoDialog(cb)
    },
    centerCoverIdSelectCallback() {

    },
    lookCenterCoverInfo() {

    },
    modelIdSelectCallback() {
      if (this.R.isNil(this.dataForm.modelId)) {
        this.dataForm.leftSuccessMo = 0
        this.dataForm.leftPrimaryMo = 0
        this.dataForm.leftJiaZi = 0
        this.dataForm.leftKouZi = 0
        this.dataForm.leftLengQue = 0
        this.dataForm.leftLouDou = 0
        this.dataForm.leftMenTou = 0
        this.dataForm.leftQiTou = 0
        this.dataForm.leftXinZi=0
        return
      }
      mRequest('modelApi/modelInfoInfo', {id: this.dataForm.modelId}, (res) => {
        const {data} = res
        const {modelInfo} = data
        const {successMo, primaryMo, jiaZi, kouZi, lengQue, louDou, menTou, qiTou, xinZi,modelShelfNo} = modelInfo
        this.dataForm.leftSuccessMo = successMo
        this.dataForm.leftPrimaryMo = primaryMo
        this.dataForm.leftJiaZi = jiaZi
        this.dataForm.leftKouZi = kouZi
        this.dataForm.leftLengQue = lengQue
        this.dataForm.leftLouDou = louDou
        this.dataForm.leftMenTou = menTou
        this.dataForm.leftQiTou = qiTou
        this.dataForm.leftXinZi = xinZi
        this.dataForm.modelShelfNo = modelShelfNo
      })
    },
    addNewModel() {
      const cb = (newValue) => {
        this.initModelInfoList('modelId')
        this.dataForm.modelId = newValue.id
      }
      this.$parent.openModelInfoDialog(cb)
    },
    // addNewBottle() {
    //   const cb = () => {
    //     this.initBottleSuitList()
    //   }
    //   this.$parent.openSysDicDialog(cb)
    // },
    addNewOrderModel() {
      const property={
        'productId': this.dataForm.productId,
        'modelId': this.dataForm.modelId,
      }
      this.$parent.openOrderModelDialog(property)
    },
    /**
     * 新增产品
     */
    addNewProduct() {
      let clientId = this.dataForm.clientId
      const cb = (newValue) => {
        this.initProductInfoList()
        this.dataForm.productId = newValue.id
     //   this.productSelectCallback(this.dataForm.productId)
      }
      this.$parent.openProductInfoDialog({'clientId':clientId},cb)
    },


     lookProductInfo(item) {
       const cb = (newValue) => {
         this.initProductInfoList()
         this.productSelectCallback(this.dataForm.productId)
       }
       this.$parent.openProductInfoDialog2(this.dataForm.productId,cb)
    },
    /**
     * 新增加工类型
     */
    addNewDeepType() {
      const cb = (newValue) => {
        this.initDeepCategoryList('deepCategoryList')
        this.dataForm.deepCategoryList = newValue.id
      }
      this.$parent.openDeepCategoryInfoDialog(cb)
    },
    /**
     * 新增客户信息
     */
    addNewClientInfo() {
      const cb = (newValue) => {
        this.initClientInfoList()
        this.dataForm.clientId = newValue.id
      }
      this.$parent.openClientInfoDialog(cb)
    },
    init(id, property, cb, submitAtFinish = true) {
      this.dataForm.id = id || 0

    //  this.initBottleSuitList()
   //   this.initModelInfoList('modelId')
      this.initDeepCategoryList('deepCategoryList')
      this.initProductInfoList()
      this.initTradeOrderStatusList('status')
      this.initRelateDeepCategoryList('relateDeepCategoryList')

      //  this.initProductDeviceListByParent('deviceId')
/*      this.initBottleCapInfoList('bottleCapId')
      this.initCenterCoverInfoList('centerCoverId')
      this.initSprayHeadInfoList('bottleSprayHeadId')
      this.initNeckRingInfoList('bottleNeckRingId')*/
      this.$refs.dialog.initDialog({
        submitAtFinish,
        dataForm: this.dataForm,
        config: this.dataFormConfig,
      /*  stepsConfigRow: this.stepsConfigRow,*/
        dataRule: this.getNeedDataRule('orderTime', 'clientId', 'deliveryTime', 'productId', 'deviceId', 'orderProductNumber', 'boxSupplyWay', 'unitPrice', 'boxNumber', 'trayNumber', 'specialMark',
          'deepType', 'bottleLength', 'bottleWeight', 'bottleHeight', 'modelId', 'bottleOutDiameter',
          'packRequire', 'status'),
        initApi: 'orderApi/orderDetailInfo',
        saveApi: 'orderApi/saveOrderDetail',
        updateApi: 'orderApi/updateOrderDetail',
        objectName: 'orderDetail',
        submitCallback: cb
      })

      this.$nextTick(() => {
        if (!this.R.isNil(property)) {
          Object.keys(property).forEach((key) => {
            this.dataForm[key] = property[key]
          })
        }
      })



    }
  }
}
</script>
